package com.test.schedule.DateTest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MyDataUtils {
    public static ThreadLocal<Map<String,SimpleDateFormat>> threadLocal =new ThreadLocal<Map<String,SimpleDateFormat>>(){
        @Override
        protected Map<String,SimpleDateFormat> initialValue(){
            return new HashMap();
        }
    };
    public static Date str2Date(String str,String sdf){
        if(null != str && !"".equals(str)){
            Date date = null;
            try{
                date = getSimpleDateFormat(sdf).parse(str);
                return date;
            }catch(ParseException var4){
                var4.printStackTrace();
                return null;
            }
        }else{
            return  null;
        }
    }
    private static SimpleDateFormat getSimpleDateFormat(String pattern){
        Map<String,SimpleDateFormat> simpleDateFormatMap = (Map)threadLocal.get();
        SimpleDateFormat format = (SimpleDateFormat)simpleDateFormatMap.get(pattern);
        if(format == null){
            format = new SimpleDateFormat(pattern);
            simpleDateFormatMap.put(pattern,format);
        }
        return format;
    }
}
